

local Tool=_G.CommonOHOH.load.WeaponTool();

local function_list =_G.ClientOHOH.function_list
local M=_G.ClientOHOH.class.new(script, _G.ClientOHOH.files.BaseComponent());

-- WeaponStoreGui= _G.ClientOHOH.load.UIManager():CreateUI("WeaponStoreGui",require(script.Parent.WeaponStoreGui))
WeaopnEquipGui= _G.ClientOHOH.load.UIManager():CreateUI("WeaopnEquipGui",require(script.Parent.WeaopnEquipGui))
WeaponMakeGui= _G.ClientOHOH.load.UIManager():CreateUI("WeaponMakeGui",require(script.Parent.WeaponMakeGui))
-- -- WeaponUpgradeGui= _G.ClientOHOH.load.UIManager():CreateUI("WeaponUpgradeGui",require(script.Parent.WeaponUpgradeGui))


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);	
	self.ui_data={}
	self.WeaponCtrl=self.parent:GetComponent(_G.CommonOHOH.load.Common_WeaponStoreCtrl())
	self.WeaponCtrl.WeaponUpdateEvent:Connect(function()
		self:Update(self.WeaponCtrl:GetWeapon(),self.WeaponCtrl:GetEquip(),self.WeaponCtrl:GetBroEquip())
	end)
	
	self:Update(self.WeaponCtrl:GetWeapon(),self.WeaponCtrl:GetEquip(),self.WeaponCtrl:GetBroEquip())
	_G.ClientOHOH.load.UIManager().ui_list.HomeGui:SetWeaponRed()

	self:MonitorGui()

	--监听显示 武器制作
	local pp=_G.ClientOHOH:GetPath_Loop("NPC.Weapon.att.ProximityPrompt",game.Workspace)
	pp.ActionText=_G.LanguageHelper:GetText(8888008)
	pp.Triggered:Connect(function()
		WeaponMakeGui:Show();
	end)

end;

function M.dtor(self)
	M.super.dtor(self);
end;


function M.CeateCellModel(self,guiId,wid,data,ismyEquip,i,broId)

	return {
		isEquip=ismyEquip,
		id=guiId,
		index=i,
		cfgId=data.cfgId,
		weaponId=wid,
		name="Weapon"..data.cfgId,
		icon=data.icon,
		title=data.titleName,
		titleId=data.titleId,
		level=data.level,
		value=data.power,
		nextValue=data.nextPower,
		merge=data.upgrade,

		userId=self:FindEquipUserId(broId),--使用者Id

		RemoveEvent=_G.CommonOHOH.load.Connect().new(),
		UpdateEvent=_G.CommonOHOH.load.Connect().new(),

		dtor=function(this)
			this.RemoveEvent:dtor()
			this.UpdateEvent:dtor()
		end,

		Upd=function(this,isequip,bId)

			local  userId=self:FindEquipUserId(bId)--使用者Id
			this.isEquip=isequip
			this.userId=userId
			this.UpdateEvent:Action()
		end
	}
	
end


function M.FindEquipUserId(self,broId)

	if  not broId then
			return
	end
	if broId=="Myself" then
		return game.Players.LocalPlayer.UserId
	else
		return tonumber(self.WeaponCtrl:GetBroInfoByBroId(broId).userId)
	end
end

function M.Update(self,data,equipWeaponId,broEuqip)
	local nowList={}
	local newCount=0
	
	local  weap2BroId={}

	for broId,wid in pairs(broEuqip) do
		if not weap2BroId[wid] then
			weap2BroId[wid]={}
		end
		table.insert(weap2BroId[wid],broId)
	end

	-- print("-------KKKKKKKKKK----------好友数据发生更新!")
	-- _G.CommonOHOH:PrintTable(broEuqip)

	-- _G.CommonOHOH:PrintTable(weap2BroId)

	for k,v in pairs(data) do
		
		for i=1,v.count do
			local nId=k.."_"..i

			local isEquip=equipWeaponId==k

			local broId=nil
			if i==1 and isEquip then
				broId="Myself"
			else
				
				local wdata=weap2BroId[k]
				if wdata and #wdata>0 then
					 broId= wdata[1]
					table.remove(wdata,1)
				end
	
			end


			nowList[nId]=true
			if self.ui_data[nId] then
				self.ui_data[nId]:Upd(isEquip,broId)
				-- self.ui_data[nId].UpdateEvent:Action(isEquip,i)
				-- if isEquip and not self.EquipData then
				-- 	self.EquipData=self.ui_data[nId]
				-- end
				-- continue
			else
				newCount=newCount+1

				self.ui_data[nId]=self:CeateCellModel(nId,k,v,isEquip,i,broId)
				-- if isEquip  and not self.EquipData then
				-- 	self.EquipData=self.ui_data[nId]
				-- end
			
			end
		end
	end



	for k,v in pairs(self.ui_data) do --清空多余的
		if not nowList[k] then
			newCount=newCount-1
			v.RemoveEvent:Action()
			v:dtor()
			self.ui_data[k]=nil
		end
	end

	if newCount>0 then
		_G.ClientOHOH.load.UIManager().ui_list.HomeGui:SetWeaponRed(newCount)
	end

	WeaopnEquipGui:UpdateData(self.ui_data)
	WeaponMakeGui:UpdateData(self.ui_data)

	
end

function M.MonitorGui(self)

	WeaopnEquipGui.EquipBestEvent:Connect(function()
		-- local oldEquip=self.EquipData
		-- self.EquipData=nil
		-- local r = self.WeaponCtrl:EquipBest()
		-- if r then
		-- 	oldEquip.isEquip=false
		-- 	oldEquip.UpdateEvent:Action()
		-- else
		-- 	self.EquipData=oldEquip
		-- end

			return  self.WeaponCtrl:EquipBest()
	end)

	WeaopnEquipGui.RemoveEvent:Connect(function(list)

		--重组数据格式：{"weaponId"=10}
		local data={}
		for _,v in pairs(list) do
			if not data[v.weaponId] then
				data[v.weaponId]=1
			else
				data[v.weaponId]=data[v.weaponId]+1
			end
		end

		return self.WeaponCtrl:Remove(data)
	end)	

	WeaponMakeGui.MergeEvent:Connect(function(list)
		--合并武器:weaponId
		return self.WeaponCtrl:Upgrade(list)
		
	end)

	WeaopnEquipGui.EquipEvent:Connect(function(data,broId)
		return self.WeaponCtrl:EquipWeapon(data.weaponId,broId)
		-- self.EquipData.isEquip=false
		-- self.EquipData.UpdateEvent:Action()
		-- if r then
		-- 	data.isEquip=true
		-- 	data.UpdateEvent:Action()
		-- 	self.EquipData=data
		-- end 
	end)

end

return M